﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Space_Conquest
{
    class GameInitializer
    {

        /// <summary>
        /// Initial settings
        /// </summary>
        public GameInitializer()
        {

            IsMultiplayer = false;
            IsServer = false;
            MapFileName = "DefaultMap";
            NumberOfPlayers = 2;
            ServerIP = "127.0.0.1";
            IsMapEditor = false;

        }

        /// <summary>
        /// Is this game multiplayer (LAN), or single player (vs. computer)
        /// </summary>
        public bool IsMultiplayer;

        /// <summary>
        /// The filename (no extension or directories) of the map file in Maps folder
        /// </summary>
        public string MapFileName;

        /// <summary>
        /// Number of players in game, must be 2-4
        /// </summary>
        public int NumberOfPlayers;
        
        /// <summary>
        /// If multiplayer, is this machine the game's server?
        /// </summary>
        public bool IsServer;

        /// <summary>
        /// If multiplayer and Server = false, what is IP of server to connect to?
        /// </summary>
        public string ServerIP;

        /// <summary>
        /// The names of players in game, PlayerNames.Count must = NumberOfPlayers
        /// </summary>
        public List<string> PlayerNames = new List<string>();

        /// <summary>
        /// Should it start as the Map Editor?
        /// </summary>
        public bool IsMapEditor;

        public void ValidateSettings()
        {

            // NumberOfPlayers much match the number of player names created
            if (NumberOfPlayers != PlayerNames.Count)
                throw new Exception("NumberOfPlayers does not match PlayerNames.Count");

            // If client, ServerIP must not be empty, and must be valid IP format
            if((!IsServer) && (ServerIP == "" || !Helpers.ValidIPAdress(ServerIP) ))
                throw new Exception("Trying to be client, but IP is not valid.");

            // Cut down MapFileName since MapLoader doesn't need directory or extension
            if(MapFileName != "")
                MapFileName = Path.GetFileNameWithoutExtension(MapFileName);

        }

    }
}
